﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Messaging;



namespace _012_MSMQClient
{
    public class MessageArray {

    public int[] Array { get;set;}
    public String Queue { get;set;}

    
    }

    public class MessageIndex
    {

        public int Index { get; set; }
        public String Queue { get; set; }

        public int Command { get; set; }


    }

    class Program
    {
        static string client_name = @".\private$\input";
        static string server_name = @".\private$\client1";

        static void Main(string[] args)
        {
            try
            {
                if (!MessageQueue.Exists(client_name)) MessageQueue.Create(client_name);
                if (!MessageQueue.Exists(server_name)) MessageQueue.Create(server_name);

                MessageQueue queue_client = new MessageQueue(client_name);
                MessageQueue queue_server = new MessageQueue(server_name);

                queue_client.Formatter = new XmlMessageFormatter(new Type[] { typeof(MessageArray), typeof(MessageIndex) });
                queue_server.Formatter = new XmlMessageFormatter(new Type[] { typeof(MessageArray), typeof(MessageIndex) });

                int command = 0;
                HashSet<int> requests = new HashSet<int>();

                do
                {
                    Console.WriteLine("Menu");
                    Console.WriteLine("1 - Exit");
                    Console.WriteLine("2 - Add Array");
                    Console.WriteLine("3 - Show Servers Array");
                    Console.WriteLine("4 - Process");
                    Console.WriteLine("5 - Show result");

                    command = Int32.Parse(Console.ReadLine());

                    switch (command)
                    {
                        case 2:
                            Console.WriteLine("Enter array size:");
                            int size = Int32.Parse(Console.ReadLine());
                            if (size > 0)
                            {
                                MessageArray msg_request = new MessageArray() { Array = new int[size], Queue = server_name };

                                for (int i = 0; i < size; i++)
                                {
                                    Console.Write("Enter element {0}:", i);
                                    msg_request.Array[i] = Int32.Parse(Console.ReadLine());
                                }
                                Console.Write("Sending request to server ...");
                                queue_client.Send(new Message(msg_request));
                                Message msg_answer = queue_server.Receive();

                                if (msg_answer.Body is MessageIndex)
                                {
                                    MessageIndex rq_index = (MessageIndex)msg_answer.Body;
                                    Console.WriteLine("{0}", rq_index.Index);
                                    requests.Add(rq_index.Index);
                                }
                                else Console.WriteLine("Wrong message type");
                            }
                            break;
                        case 3:
                        case 5:
                            {
                                Console.WriteLine("We have following requests:");
                                foreach (int rq_id in requests) Console.Write("{0} ", rq_id);
                                Console.WriteLine();
                                int rq_index = Int32.Parse(Console.ReadLine());
                                queue_client.Send(new Message(new MessageIndex() { Queue = server_name, Command = command, Index = rq_index }));

                                Message msg_answer = queue_server.Receive();
                                if (msg_answer.Body is MessageArray)
                                {
                                    MessageArray array = (MessageArray)msg_answer.Body;
                                    foreach (int a in array.Array) Console.Write("{0} ", a);
                                    Console.WriteLine();
                                }
                                else Console.WriteLine("Wrong message type");

                            }
                            break;
                        case 4:
                            {
                                Console.WriteLine("We have following requests:");
                                foreach (int rq_id in requests) Console.Write("{0} ", rq_id);
                                Console.WriteLine();
                                int rq_index = Int32.Parse(Console.ReadLine());
                                queue_client.Send(new Message(new MessageIndex() { Queue = server_name, Command = command, Index = rq_index }));
                                Console.WriteLine();
                            }
                            
                            break;
                    }
                } while (command != 1);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }
}
